/*
 * @Author: 肖思汗 
 * @Date: 2025-06-04 15:35:28 
 * @Last Modified by: 肖思汗
 * @Last Modified time: 2025-07-16 11:09:45
 */

import { autorun, makeAutoObservable } from "mobx";
import userStore from "src/pages/User/userStore";
import { GoodsCategoryData, GoodsData } from "types/type";
import { get, isUndefined } from "lodash";
import categoryApi from "./categoryApi";
//@ts-ignore
import indexApi from "../Index/indexApi";

class CategoryStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
        autorun(() => {
            if (userStore.isLogin) {
                //商品一级分类菜单
                indexApi.firstProductType().then(res => {
                    this.setGoodsCategoryDatas(res.data);
                })
                //获取所有的商品二级分类菜单
                indexApi.secondProductType().then(res => {
                    this.setSecondProductTypes(res.data);
                });

            }
        }, { delay: 30 });
    }

    isInit = true;
    init() {
        if (this.isInit) {
            this.isInit = false;
            this.getNextGoodsList(true);
        }
    }

    // 是否是切换款式(编辑器切换款式的时候, 这个状态为true)
    isSwitchKua: boolean = false;
    setIsSwitchKua(isSwitchKua: boolean) {
        this.isSwitchKua = isSwitchKua;
    }

    // 选中的商品id
    activeGoodsId?: number;
    setActiveGoodsId(activeGoodsId?: number) {
        this.activeGoodsId = activeGoodsId;
    }

    // 商品一级分类
    goodsCategoryDatas: Array<GoodsCategoryData> = [];
    setGoodsCategoryDatas(goodsCategoryDatas: Array<GoodsCategoryData>) {
        this.goodsCategoryDatas = goodsCategoryDatas;
        // const productTypeId = get(goodsCategoryDatas, [0, "id"], -1) as number;
        // if (productTypeId) {
        //     this.setActiveFirstProductType(productTypeId);
        // }
    }

    //选中的一级分类
    activeFirstProductType?: number;
    setActiveFirstProductType(activeFirstProductType?: number) {
        this.activeFirstProductType = activeFirstProductType;
        this.activeSecondProductType = [];
        this.getNextGoodsList(true);
    }

    // 商品二级分类
    secondProductTypes: Array<GoodsCategoryData> = [];
    setSecondProductTypes(secondProductTypes: Array<GoodsCategoryData>) {
        this.secondProductTypes = secondProductTypes;
        this.activeSecondProductType = [];
        // const productTypeId = get(secondProductTypes, [0, "id"], -1) as number;
        // if (productTypeId) {
        //     this.setActiveSecondProductType(productTypeId);
        // }
        // 默认不选二级

    }

    // 选中的二级分类
    activeSecondProductType: Array<number> = [];
    // 切换二级分类(单选逻辑)
    setActiveSecondProductType(activeSecondProductType: number) {
        if (this.activeSecondProductType.includes(activeSecondProductType)) {
            //this.activeSecondProductType = this.activeSecondProductType.filter(item => item !== activeSecondProductType);
            this.activeSecondProductType = [];
        } else {
            // 多选改单选
            this.activeSecondProductType = [activeSecondProductType];
            // this.activeSecondProductType = [...this.activeSecondProductType, activeSecondProductType];
        }
        this.getNextGoodsList(true);
    }

    //价格带
    priceZones: Array<{ min: number, max: number }> = [];
    seelctPriceZone(priceZone: { min: number, max: number }) {
        if (this.priceZones.some(item => item.min === priceZone.min && item.max === priceZone.max)) {
            this.priceZones = this.priceZones.filter(item => item.min !== priceZone.min && item.max !== priceZone.max);
        } else {
            this.priceZones = [...this.priceZones, priceZone];
        }
        this.getNextGoodsList(true);

    }

    loading: boolean = false;
    page: number = 1;//当前页面
    pageSize: number = 8;//分页数
    total: number = 0;//总数
    goodsList: Array<GoodsData> = [];//数据列表
    hasMore: boolean = true;//是否有更多数据

    searchValue = "";// 搜索词
    setSearchValue(searchValue: string) {
        this.searchValue = searchValue;
    }

    //获取下一页的数据
    async getNextGoodsList(reset = false) {

        if (reset) {
            this.page = 1;
            this.hasMore = true;
        } else if (!this.hasMore || this.loading) {
            return;
        } else {
            this.page++;
        }
        this.loading = true;

        categoryApi.queryCategoryProduct({
            pageNum: this.page,                              // 页码
            pageSize: this.pageSize,                        // 每页条数
            productName: this.searchValue,                  // 商品名称
            //@ts-ignore
            firstProductTypeId: this.activeFirstProductType,            // 一级分类
            secondProductTypeIds: this.activeSecondProductType,         // 二级分类
            priceZones: this.priceZones  // 价格带
            // priceZones: [{
            //     min: 0,
            //     max: 1
            // }]
        }).then(async res => {

            if (reset) {
                this.goodsList = [];
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                this.goodsList = [...res.rows];
                this.activeGoodsId = undefined;

            } else {
                this.goodsList = [...this.goodsList, ...res.rows];
            }
            this.total = res.total;
            this.hasMore = (res.rows.length === this.pageSize);
            // await new Promise(resolve => setTimeout(resolve, 100));
            this.loading = false;
        });

    }


}

const categoryStore = new CategoryStore();

export default categoryStore;